<template>
  <el-dialog :visible.sync="isShow" width="600" center class="dialog">
    <el-form :model="form" :rules="rules" ref="form" label-width="100px" inline>
      <el-form-item label="用户名" prop="username">
        <el-input v-model="form.username"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="form.email"></el-input>
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input v-model="form.phone"></el-input>
      </el-form-item>
      <el-form-item label="角色" prop="role_id">
        <el-select v-model="form.role_id">
          <el-option label="管理员" :value="2"></el-option>
          <el-option label="老师" :value="3"></el-option>
          <el-option label="学生" :value="4"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="form.status">
          <el-option label="启用" :value="1"></el-option>
          <el-option label="禁用" :value="0"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div class="dialog-header" slot="title">
      <span class="title">{{ mode == "add" ? "新增用户" : "编辑用户" }}</span>
      <span class="close el-icon-close" @click="isShow = false"></span>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="isShow = false">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { addUser, editUser } from "@/api/userlist.js";
export default {
  props: ["mode", "userInfo"],
  watch: {
    isShow() {
      if (!this.isShow) {
        this.form = null;
        this.form = {
          username: "", //	是	string	用户名
          email: "", //	是	string	邮箱
          phone: "", //	是	string	手机号
          role_id: "", //	是	string	角色 、2 管理员、3 老师、4 学生
          status: "", //	否	string	1(启用)0(禁用)
          remark: "", //	否	string	备注
        };
        this.$refs.form.resetFields();
      }
      if (this.mode == "edit") {
        this.form = null;
        console.log("编辑", this.userInfo);
        this.form = this.userInfo;
      }
    },
  },
  data() {
    return {
      isShow: false,
      form: {
        username: "", //	是	string	用户名
        email: "", //	是	string	邮箱
        phone: "", //	是	string	手机号
        role_id: "", //	是	string	角色 、2 管理员、3 老师、4 学生
        status: "", //	否	string	1(启用)0(禁用)
        remark: "", //	否	string	备注
      },
      rules: {
        username: [{ required: true, message: "请输入", trigger: "change" }],
        email: [{ required: true, message: "请输入", trigger: "change" }],
        phone: [
          { required: true, message: "请输入", trigger: "change" },
          {
            min: 6,
            max: 18,
            message: "长度在 6 到 18 个字符",
            trigger: "blur",
          },
        ],
        role_id: [{ required: true, message: "请输入", trigger: "change" }],
        status: [{ required: true, message: "请输入", trigger: "change" }],
        remark: [{ required: true, message: "请输入", trigger: "change" }],
      },
    };
  },
  methods: {
    submit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.mode == "edit") {
            console.log("bianjiye");
            editUser(this.userInfo)
              .then(() => {
                this.$message.success("编辑成功");
                this.isShow = false;
                this.$emit("getData");
              })
              .catch((err) => {
                this.$message.error(err);
              });
          } else if (this.mode == "add") {
            addUser(this.form)
              .then(() => {
                this.$message.success("新增成功");
                this.isShow = false;
                this.$emit("getData");
              })
              .catch((err) => {
                this.$message.error(err);
              });
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

<style lang="less">
.dialog {
  .dialog-header {
    height: 54px;
    background: linear-gradient(to right, #02c5fa, #1294fa);
    font-size: 18px;
    font-family: Microsoft YaHei Regular, Microsoft YaHei Regular-Regular;
    font-weight: bold;

    color: #ffffff;
    letter-spacing: 0px;
    .title {
      line-height: 54px;
    }
    .close {
      float: right;
      padding: 20px 30px 0 0;
    }
  }
}
</style>
